package org.example.subside.Play.ReadLocalFile;

import lombok.extern.slf4j.Slf4j;

import java.io.File;

/**
 * @Author : zhangbo
 * @data : 2024/6/7
 * @description :
 */
@Slf4j
public class ReadFileUtils {


    // 定义静态的文件后缀
    private static final String SUFFIX_DOC = ".doc";
    private static final String SUFFIX_DOCX = ".docx";
    private static final String SUFFIX_PDF = ".pdf";
    private static final String SUFFIX_TXT = ".txt";
    /**
     * .
     * 根据文件类型解析文件内容
     *
     * @param file 文件
     * @return 解析内容
     */
    private static String FileToString(File file) {
        StringBuilder readStr = new StringBuilder();
        String fileType = file.getName().substring(file.getName().lastIndexOf("."));
        log.debug("解析文件类型为[{}]文件", fileType);
        switch (fileType) {
            case SUFFIX_DOC: {
                readStr.append(WordUtil.readWord(SUFFIX_DOC, file.getAbsolutePath()));
                break;
            }
            case SUFFIX_DOCX: {
                readStr.append(WordUtil.readWord(SUFFIX_DOCX, file.getAbsolutePath()));
                break;
            }
            case SUFFIX_PDF: {
                readStr.append(PDFToWordUtil.getTextFromPdf(file.getAbsolutePath()));
                break;
            }
            case SUFFIX_TXT: {
                readStr.append(TxtUtil.readTxtFile(file.getAbsolutePath()));
                break;
            }
            default: {
                log.error("文件类型不正确，请上传.doc、.docx、.pdf、.txt后缀文件");
                throw new RuntimeException("文件类型不正确，请上传.doc、.docx、.pdf、.txt后缀文件");
            }
        }
        return readStr.toString();
    }
}
